Repository navigation
Reimplement UnitsState::processMeleeInitiation with the original's code, from gynt's text on main - #543
Conversation
UnitsState::processMeleeInitiation from 85.9% (gynt's text, on main since sourcehold#238) to the original's code. reccmp reads 99.0%: the seven lines it still shows are the same instructions with the same operand under another label (below), and the status line counts the function as 100% with a note that says so, as main's file does for 0x004D9290. quick_diff below is tools/reimplementation-helper/batch/quick_diff.py: this file compiled alone against the original's disassembly, every global address folded to one token. A ratio from 0 to 1, not a reccmp percentage. - The scan of the unit's own tile is a do loop behind its own test. The original loads `this` into edi in front of the loop's test (0x00549F4E) and uses it up to 0x0054A458. As a for loop MSVC loads it inside the loop and again behind the 24 tile tests, and keeps a zero in edi from the loop's end to the function's end (quick_diff 0.8976 -> 0.9399). - goto attack: when the unit already fought is still in reach, the original turns towards it and jumps into the code that starts the attack: the jmp at 0x0054A52B lands at 0x0054A6BA, which is also reached by falling through from 0x0054A6B9. With a flag set in the loop and tested behind it MSVC keeps `this` in ebx over the rest of the function, where the original reads it from the stack for each call and has 0x6a and 8 in edi and edx (0x0054A6C5). - goto noTarget: "nobody found on the 24 tiles" and "no enemy around" end in the same three stores and the same return, which the original has once: the je at 0x00549FCB lands at 0x0054A652, which is also reached by falling through from 0x0054A650. Written twice, both copies stay. - The three together: 0.9796, with the original's 734 instructions. Other forms, in quick_diff only: 0.9640 with one goto or with none (a flag that skips the second search, the attack nested in an if), 0.9244 with the attack written out at both places. The real build is of the form with the two goto. - _adjacentTiles[13] adds [SomeY + 1][5] in front of [SomeY + 1][6], as _adjacentTiles[17] does with [2] and [3]: with [6] first MSVC put the two loads the other way round than the original has them (0x0054A2C0). quick_diff folds global addresses and did not show it; the first real build did. What reccmp still shows: seven instructions (0x0054A20F, 0x0054A233, 0x0054A239, 0x0054A3D9, 0x0054A3DF, 0x0054A403, 0x0054A425) read directionTranslationMatrix one row above the current one, six written [SomeY][-1] to [-7] (gynt's negative columns of the current row) and one [SomeY - 1][0]. The matrix is the first member of TileMapState, so their displacement is TileMapState's address less 4 to 32. In the original TileMapState is at 0x01A93208 and MinimapViewState (0x01A31610, 400376 bytes) ends where it starts, so reccmp prints MinimapViewState+400372 for TileMapState - 4. In the DLL TileMapState's instance has other data in front of it and reccmp prints that: ViewportRenderState+1699844 (it is 1699848 bytes long) with the units option on, function-resolver objects with it off. The compiler's listing has TileMapState's symbol less 4 to 32 in all seven, and all 85 global memory operands of the function are the original's in the original's order. Any form that reads the row above ([SomeY][-1], [SomeY - 1][7]) comes to the same displacement (both were compiled), and the label comes from the DLL's data layout, so no source form changes these lines; main's build shows the same seven. The status line counts the function the way the file does for 0x004D9290 (sourcehold#510: the instance[i - 1] address label) and for 0x00405130, 0x004274F0 and 0x00429A40 (sourcehold#408). The rest is gynt's, the handwritten block included (7005c48 79.0%, 951fc13 84.5%, ff6859a 85.9%; main's status line still said 84.5%). The second search loses one level of nesting, so its lines show as changed. The bound of both searches is countof(field_0x11cf4[0]) in place of 24, the column count of the table they index: same object code. Measured with real builds on main 0cea04d (bundled MSVC1400-SP1, function and data resolvers on locally, reccmp), of this file together with the two /GL lines of sourcehold#541: 99.0% in a small build with OPENSHC_FUNCTION_VISIBILITY_UNITS on (519 files, 529 functions), where every other function but sourcehold#541's clearAllUnits reads as in the same build of main. With the option off the same list reads 99.0% as well, the same seven lines (and 98.8% for the text before the _adjacentTiles[13] change: those seven and that one sum). Two comments and the two loop bounds changed after the builds; every section of the object file has the same bytes and relocations as before. Co-authored-by: Gynt <gynt@users.noreply.github.com> Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
|
Style guide reading (advisory, posted once) 19 possible deviation(s) from REIMPLEMENTATION_STYLE_GUIDE.md across 1 of the 1 reimplemented The match comes first. Where the ugly form is what matched, it stays -- that is the guide's own overriding rule, and rule 21 asks for a one-line comment saying what was measured so the next reader does not retry it. This report is posted once, when the PR opens, and is not repeated on later pushes.
WhereRule 9 — Prefer
Rule 18 — Use the name, not the number
Rule 20 — Keep nesting low; no
|
gynt
left a comment
There was a problem hiding this comment.
Is there no way to avoid the do-while and the goto?
|
Not that we found. The alternatives were measured with the file compiled alone ( The The two
With a flag, If you prefer it without the |
UnitsState::processMeleeInitiation(0x00549C70) from 85.9% (reccmp; gynt's text, on main since #238) to the original's instructions. reccmp reads 99.0%: the seven lines it still shows are the same instructions with the same operand under another label ("What reccmp still shows" has the seven pairs and the arithmetic), and the status line counts the function as 100% with a note that says so, the way main's file does for 0x004D9290. The body is gynt's, the handwritten block included; four places change, and none changes what the function does.processMeleeInitiationdoloop, twogotoin place of a flag and of a second copy of three stores, the order of two terms of one sum"Before" is main's reading in a build, the same with
OPENSHC_FUNCTION_VISIBILITY_UNITSoff and on. Main's status line still says 84.5%: it is older than ff6859a, the commit that brought the text to 85.9%.What matched it
Main's text had the original's instructions in the original's order almost everywhere. What differed was where
thislives behind the handwritten block, and three things followed from that.The
quick_diffreadings below are from the repository'stools/reimplementation-helper/batch/quick_diff.py: it compiles this one file alone and compares the listing with the original's disassembly, with every global address folded to one token. It gives a ratio from 0 to 1, not a reccmp percentage.doloop behind its own test. The original loadsthisinto edi in front of the loop's test (0x00549F4E) and uses it from there up to the check of the unit already fought (0x0054A458). As aforloop MSVC loadsthisinside the loop, loads it again behind the 24 tile tests, and keeps a zero in edi from the end of the loop to the end of the function (quick_diff0.8976 -> 0.9399).goto attack. When the unit already fought is still in reach, the original turns towards it and jumps into the code that starts the attack: the jmp at 0x0054A52B lands at 0x0054A6BA, which is also reached by falling through from 0x0054A6B9. Main's text set a flag in the loop and tested it behind it. MSVC removes the flag, but then keepsthisin ebx over the rest of the function, where the original reads it from the stack for each call (0x0054A6E5, 0x0054A737) and has 0x6a and 8 in edi and edx (0x0054A6C5).goto noTarget. "Nobody found on the 24 tiles" and "no enemy around at all" end in the same three stores and the same return. The original has them once: the je at 0x00549FCB lands at 0x0054A652, which is also reached by falling through from 0x0054A650. Written twice, both copies stay._adjacentTiles[13]adds[SomeY + 1][5]in front of[SomeY + 1][6], the way_adjacentTiles[17]adds[2]in front of[3]. With[6]first MSVC put the two loads the other way round than the original has them (0x0054A2C0).quick_diffcould not show it, because it reads every global address as the same token; the first real build did.The first three together read 0.9796 in
quick_diff, with the original's 734 instructions. What it still shows there is its own spelling of two indexed globals and of the local array, and the registers of 0x6a and 8, which only the/GLbuild gives (edx holds 8 acrosssaveUnitStateBeforeInterruption).Other forms, measured
These are
quick_diffreadings of the file compiled alone, thedoloop in all of them. None of them went through a real build: the real build that reads 99.0% is of the form with the twogoto, which reads 0.9796 here.goto, the flag kept (set in the loop, tested behind it), the stores written twice: 0.9399;goto attackonly, the stores written twice: 0.9567;goto noTargetonly, the flag kept: 0.9640;goto, the call in the loop and a flag that only skips the second search, the attack nested in anifon a found target so that both exits fall into the three stores: 0.9640;goto attackinto that nestedif, no secondgoto: 0.9632;gotoand no flag, the attack written out at both places: 0.9244 (MSVC lays the shared code out at the first one).In each form with a flag
thisis back in ebx behind the second search.What reccmp still shows
Seven instructions read
directionTranslationMatrixone row above the current one. Six are written[SomeY][-1]to[SomeY][-7](gynt's "negative columns of the current row", ff6859a) and one[SomeY - 1][0]. The matrix is the first member ofTileMapState(offset 0,TileMapState.hpp:55), so withSomeY * 32in the register their displacement isTileMapState's address less 4 to 32.In the original
TileMapStateis at 0x01A93208.MinimapViewStateis at 0x01A31610 and 400376 bytes long, so it ends whereTileMapStatestarts, and reccmp names 0x01A93204 after the symbol it lies in:MinimapViewState+400372, which isTileMapState- 4.In the build
TileMapState's instance is the DLL's own, and what stands in front of it is whatever the linker put there. In the option-on build that isViewportRenderState(1699848 bytes long, soViewportRenderState+1699844is again its end less 4). In the option-off build it is function-resolver objects, and reccmp prints their names for the same seven operands (99.0% there too).The first of the seven, in the original and in the compiler's listing of this file (symbol demangled; 27865608 is 0x01A93208):
All 85 global memory operands of the function are the original's, in the original's order (the listing's symbol plus offset against the original's absolute address).
The seven pairs as reccmp prints them (option-on build)
TileMapStatemov ebx, [ecx + MinimapViewState+400372]mov ebx, [ecx + ViewportRenderState+1699844]mov ebx, [ecx + MinimapViewState+400368]mov ebx, [ecx + ViewportRenderState+1699840]add ebx, [ecx + MinimapViewState+400372]add ebx, [ecx + ViewportRenderState+1699844]mov ebx, [ecx + MinimapViewState+400348]mov ebx, [ecx + ViewportRenderState+1699820]add ebx, [ecx + MinimapViewState+400352]add ebx, [ecx + ViewportRenderState+1699824]mov ebx, [ecx + MinimapViewState+400348]mov ebx, [ecx + ViewportRenderState+1699820]mov edx, [eax + MinimapViewState+400344]mov edx, [eax + ViewportRenderState+1699816]400376 - 400372 = 4 and 1699848 - 1699844 = 4, and so on down the column: both sides are the same distance in front of
TileMapState. The seven lines are in two hunks; reccmp shows nothing else for the function.No source form can change these seven lines: any form that reads the row above (
[SomeY][-1],[SomeY - 1][7]) comes to the same displacement, and reccmp takes the label from the DLL's data layout, not from this function.[SomeY - 1][k]its listing has the same seven operands (in a body four instructions shorter: the negative columns are what give the original's instructions around them). The option-on and option-off builds print different labels for these operands, and main's own builds show the same seven lines.The status file already counts functions whose only difference is such a label as 100% with a note:
This PR writes its line the same way: "(counted: only the address labels of the seven directionTranslationMatrix[SomeY - 1] operands differ)".
Kept as it is
or edx, 0x...masks.elseis gone), so its lines show as changed; they are gynt's. That is also why the style checks list things this PR did not write:_candidateTileand_tileSlotdeclared without a value (each is assigned in both arms of theifthat follows), the placeholder namesfield_0x340,field_0x3d2,field_0x11cf4andfield_0x12054, andDAT_UnitsState::instancein a member (the original addressessubstateand the threesetFacingDirectionTowardUnitMicrocalls through the global).field_0x340holds the uid of the unit this one fights: line 460 compares it withunits[_otherUnitID].uid, and lines 557 and 570 store the two uids. Renaming it is a header change, for another PR.countof(DAT_UnitPropertiesDefinedData::instance.field_0x11cf4[0])in place of 24: the column count offield_0x11cf4[9][24], which both index with_slot. Same object code._hasEnemyOnOwnTile, a flag; the compares are on ids, tiles and counts, and ondyingandisStalkedas gynt wrote them.doloop and the twogoto) says what the other form gives.//line now.Measured
Setup: the bundled MSVC1400-SP1 against the Steam
Stronghold Crusader.exe(3bb0a8c1…5387ac5a), both resolver sets enabled locally (not committed), sourcehold's reccmp with--resolve-wrapped-calls --ignore-security-cookie. All builds are on main 0cea04d. The tree that was built is this branch's.cpptogether with the two/GLlines of #541 (clearAllUnits,computeLadderClimbPath); this function calls neither and is called by neither./GLfile of main, this function,updateUnits, which calls it, the callee closures and every file of a function-visibility group that touches them): BUILD_OK, 0 failures, 99.0%, the seven lines above and nothing else. Against the same build of main every one of the 529 functions reads the same but this one andclearAllUnits, which is Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541's.updateUnitsreads 93.2% in both._adjacentTiles[13]change (98.8%, the seven lines and that one sum) every one of the 529 functions reads the same but this one. Against main's small build with the option off (436 of the files, 446 functions) the only functions with changed code are this one and Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541's two; five more read differently with the larger file list, as Take UnitsState::clearAllUnits and computeLadderClimbPath off the /GL list (both back at 100%) #541's text lists them.countof(...). The object file of the final text was compared with that of the built text: every section has the same bytes and the same relocations..cppand its status line.gotoand thedoloop (each has its comment), the 16 hex masks of the handwritten block, and the lines listed under "Kept as it is".Other PRs
#536, #539, #540 and #541 are ours and open. None touches this file, and the status file only in other lines. #541 takes two other files off
cmake/compiler-flags-gl.txt; the builds above have its two lines in. #501 and #528 (TroopValueState) do not touch this file either.🤖 Generated with Claude Code